﻿using myOffice = Microsoft.Office.Interop;
using TpTools;
using System.Text;
using System;
using System.Windows.Forms;
using System.IO;
using TpData.Manager;
using TpContracts;

namespace Tp {

    public class WordCore {

        #region Members

        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "Tp.WordCore";

        #endregion

        #region Methods

        public void SendThisDocument(myOffice.Word.Application appWord, myOffice.Word.Document mDoc) {


            if (Office.ExistingDocumentProperty(mDoc, "MailTo")) {
                //MessageBox.Show("This function is comming soon!", mDoc.Name);
                LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "SendThisDocument: Save(" + mDoc.FullName + ")", false);
                try {
                    mDoc.Save();
                    string fileName = Tools.SaveAsPDF(mDoc);

                    myOffice.Outlook.Application olApp = Office.GetOutlookApplication();

                    if (olApp != null) {
                        string filePath = mDoc.Path;
                        string fName = Path.GetFileNameWithoutExtension(mDoc.Name);
                        fName += ".msg";
                        myOffice.Outlook.MailItem msg = GetNewMailMessage(olApp, mDoc,  fileName);
                        //msg.DeleteAfterSubmit = true;
                        LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "SendThisDocument: " + Path.Combine(filePath, fName), false);
                        msg.TaskSubject = Path.Combine(filePath, fName);
                        msg.Display(false);

                    } else {
                        MessageBox.Show("Ol is null", "Error");
                    }

                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Error");
                    LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "SendThisDocument: " + ex.Message, true);
                }
                
            }
        }

        #endregion

        #region Functions

        private myOffice.Outlook.MailItem GetNewMailMessage(myOffice.Outlook.Application olApp, myOffice.Word.Document myDoc,string pdfFile) {

            #region Members

            //string sender = "";
            string mailTo = "";
            string subject = "";

            string File1 = "";
            string File2 = "";
            string File3 = "";
            string File4 = "";
            string File5 = "";
            string File6 = "";
            string File7 = "";
            string File8 = "";
            string File9 = "";

            #endregion

            #region New Message

            myOffice.Outlook.MailItem msg = olApp.CreateItem(myOffice.Outlook.OlItemType.olMailItem);
            //log.Add("GetNewMailMessage Msg.ID (" + msg.ConversationID.ToString() + ")");

            if (msg == null) {
                MessageBox.Show("Msg is null", "Error");
            }

            #endregion

            #region Sender

            //if (Office.ExistingDocumentProperty(myDoc, "Sender")) {
            //    sender = myDoc.CustomDocumentProperties["Sender"].Value.ToString();
            //    myOffice.Outlook.PropertyAccessor oPa = msg.PropertyAccessor;
            //    string senderID = "neubauer@iqsol.at";
            //    myOffice.Outlook.AddressEntry adEntry = olApp.Session.GetAddressEntryFromID(senderID);
            //    adEntry.Address = "neubauer@iqsol.at";
            //    adEntry.Name = "Neubauer Aluminium";
            //}

            #endregion

            #region MailTo

            if (Office.ExistingDocumentProperty(myDoc, "MailTo")) {
                mailTo = myDoc.CustomDocumentProperties["MailTo"].Value.ToString();
                msg.To = mailTo;
            }

            if (Office.ExistingDocumentProperty(myDoc, "Subject")) {
                subject = myDoc.CustomDocumentProperties["Subject"].Value.ToString();
                msg.Subject = subject;
            }

            msg.Attachments.Add(pdfFile);

            #endregion

            #region ccTo

            StringBuilder ccTo = new StringBuilder();

            if (Office.ExistingDocumentProperty(myDoc, "ccTo1")) {
                ccTo.Append(myDoc.CustomDocumentProperties["ccTo1"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo2")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo2"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo3")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo3"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo4")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo4"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo5")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo5"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo6")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo6"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo7")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo7"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo8")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo8"].Value.ToString());
            }

            if (Office.ExistingDocumentProperty(myDoc, "ccTo9")) {
                ccTo.Append(";" + myDoc.CustomDocumentProperties["ccTo9"].Value.ToString());
            }

            if (ccTo.Length > 0) {
                msg.CC = ccTo.ToString();
            }
            #endregion

            #region Files

            try {
                if (Office.ExistingDocumentProperty(myDoc, "File1")) {
                    File1 = myDoc.CustomDocumentProperties["File1"].Value.ToString();
                    msg.Attachments.Add(File1);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File2")) {
                    File2 = myDoc.CustomDocumentProperties["File2"].Value.ToString();
                    msg.Attachments.Add(File2);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File3")) {
                    File3 = myDoc.CustomDocumentProperties["File3"].Value.ToString();
                    msg.Attachments.Add(File3);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File4")) {
                    File4 = myDoc.CustomDocumentProperties["File4"].Value.ToString();
                    msg.Attachments.Add(File4);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File5")) {
                    File5 = myDoc.CustomDocumentProperties["File5"].Value.ToString();
                    msg.Attachments.Add(File5);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File6")) {
                    File6 = myDoc.CustomDocumentProperties["File6"].Value.ToString();
                    msg.Attachments.Add(File6);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File7")) {
                    File7 = myDoc.CustomDocumentProperties["File7"].Value.ToString();
                    msg.Attachments.Add(File7);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File8")) {
                    File8 = myDoc.CustomDocumentProperties["File8"].Value.ToString();
                    msg.Attachments.Add(File8);
                }

                if (Office.ExistingDocumentProperty(myDoc, "File9")) {
                    File9 = myDoc.CustomDocumentProperties["File9"].Value.ToString();
                    msg.Attachments.Add(File9);
                }
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "GetNewMailMessage: " + ex.Message, true);
            }

            #endregion

            return msg;

        }

        #endregion

    }
}
